from xml.etree.ElementTree import ElementTree, dump, fromstring



class RecipeXml(object):
    def __init__(self, xmlstr = None):
	self.Version      = ''
	self.Destination  = []

	if xmlstr:
	    self.__loadfromstring(xmlstr)


    def __load(self, element = None):
	if element != None:
#			try:
    	    recipe = element.find("Recipe")
	    if recipe != None:
		self.Version = recipe.attrib['ApiVer']
		destinations = recipe.findall("Destination")
		for d in destinations:				
		    self.Destination.append(TranRecipeDestination(d))

#			except:
#				pass

	    else:
		return None

    def __loadfromstring(self, xmlstr = ''):
	if xmlstr != '':
#			try:
	    self.__load(fromstring(xmlstr))
#			except:
#				pass


class TransRecipeDely(object):
    def __init__(self, element = None):
	self.Type   = ''
	self.Path   = ''
	



class TransRecipeDelyFtp(object):
    def __init__(self, element = None):
	self.Type   = 'FTP'
	self.Host   = ''
	self.User   = ''
	self.Passwd = ''
	self.Path   = ''
    
	if element:
	    self.__load(element)

    def __load(self, element = None):
#			try:
	self.Host   = element.find('Host').text
	self.User   = element.find('User').text
	self.Passwd = element.find('Passwd').text
	self.Path   = element.find('Path').text 

#			except:
#				pass

class TransRecipeDelyLocal(object):
    def __init__(self, element = None):
	self.Type = 'Local'
	self.Path = ''
		
	if element:
	    self.__load(element)

	def __load((self, element = None):
            if element != None:
		self.Path = element.find('Path').text




class TransRecipeSegments(object):
    def __init__(self, inpoint='', outpoint='', basename=''):
	self.In  = inpoint
	self.Out = outpoint
	self.Basename = basename


class TransRecipeCarbon(object):
    def __init__(self, element = None):
	self.Ext         = ''
	self.Guid   	 = ''
	self.SegmentType = 'None'
	self.Basename	 = ''
	self.Segments    = []

	if element:
	    self.__load(element)

    def __load(self, element = None):
	if element != None:
#			try:
	    self.Guid = element.find('Guid').text
	    self.Ext  = element.find('Ext').text

	    SegElement = element.find('Segments')
	    if SegElement.attrib['Type'] == "None":
		self.SegmentType = "None"
		base    = SegElement.find('Basename')
		if base != None:
                    self.Basename = base.text
	    elif SegElement.attrib['Type'] == "Stich":
	        self.SegmentType = "Stich"
	        base    = SegElement.find('Basename')
		if base != None:
		    self.Basename = base.text	
		    segments = SegElement.findall('Segment')
		    for s in segments:
			self.Segments.append(TransRecipeSegments(s.attrib['In'], s.attrib['Out']))
	    
	    elif SegElement.attrib['Type'] == "Segment":
		self.SegmentType = "Segment"
		segments = SegElement.findall('Segment')
                for s in segments:
                    self.Segments.append(TransRecipeSegments(s.attrib['In'], s.attrib['Out'], s.attrib['Basename']))

			
#			except:
#				pass



class TranRecipeDestination(object):
    def __init__(self, element = None):
	self.Name            = ''
	self.Transcode       = False
	self.Engine	     = ''
	self.Dely	     = []
		
	if (element):
	    self.__load(element)
	
    def __load(self, element = None):
	if element != None:
	    self.Name = element.attrib['Name']
	    if element.attrib['Transcode'] == 'True':
		self.Transcode = True
		self.Engine    = element.attrib['Engine']
		if self.Engine == 'Carbon':
		    self.transcoder = TransRecipeCarbon(element.find('Carbon'))
	    else:
		self.Transcode = False

	    dely = element.findall('Dely')
	    for d in dely:
		if d.attrib['Type'] == 'FTP':
		    self.Dely.append(TransRecipeDelyFtp(d))				
		else
		    self.Dely.append(TransRecipeDelyLocal(d)
